package model;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class Courriel {

public void sendMail(String destinataire, String contenu ){
		
		try {
			
			   String smtpHost = "smtp.gmail.com";
			   String from = "inscription.econf@gmail.com";
			   String to = destinataire;
			   String username = "inscription.econf@gmail.com";
			   String password = "3conf!38";

			   Properties props = new Properties();
			   props.put("mail.smtp.host", smtpHost);
			   props.put("mail.smtp.auth", "true");

			   Session session = Session.getDefaultInstance(props);
			   session.setDebug(true);

			   MimeMessage message = new MimeMessage(session);   
			   message.setFrom(new InternetAddress(from));
			   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			   message.setSubject("<NoReply> Validation inscritpion");
			   message.setText(contenu);

			   Transport tr = session.getTransport("smtp");
			   tr.connect(smtpHost, username, password);
			   message.saveChanges();

			   // tr.send(message);
			   /** Genere l'erreur. Avec l authentification, oblige d utiliser sendMessage meme pour une seule adresse... */

			   tr.sendMessage(message,message.getAllRecipients());
			   tr.close();
			
		}
			   catch (AddressException e) {
				   	e.printStackTrace();
				   } 
				   catch (MessagingException e) {
				   	e.printStackTrace();
				   }
	}

public void sendInvitation(String destinataire, String date, String conferencier, String sujet ){
	try {
		
		   String smtpHost = "smtp.gmail.com";
		   String from = "invitation.econf@gmail.com";
		   String to = destinataire;
		   String username = "inscription.econf@gmail.com";
		   String password = "3conf!38";

		   Properties props = new Properties();
		   props.put("mail.smtp.host", smtpHost);
		   props.put("mail.smtp.auth", "true");

		   Session session = Session.getDefaultInstance(props);
		   session.setDebug(true);

		   MimeMessage message = new MimeMessage(session);   
		   message.setFrom(new InternetAddress(from));
		   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		   message.setSubject("<NoReply> Confirmation participation");
		   message.setText("Bonjour," +"\n" +
		   "Nous vous informons que vous êtes conviez à la conférence donnée par" +" " +conferencier +" et qui se déroulera le" +" " +date + "."+"\n"+
		   "Sujet de la conférence:" +" " +sujet +"\n"+
		   	"\n"+
			"l'équipe Econf" );

		   Transport tr = session.getTransport("smtp");
		   tr.connect(smtpHost, username, password);
		   message.saveChanges();

		   // tr.send(message);
		   /** Genere l'erreur. Avec l authentification, oblige d utiliser sendMessage meme pour une seule adresse... */

		   tr.sendMessage(message,message.getAllRecipients());
		   tr.close();
		
	}
		   catch (AddressException e) {
			   	e.printStackTrace();
			   } 
			   catch (MessagingException e) {
			   	e.printStackTrace();
			   }
	}
}
	
	

